package com.xuxiao.event;

import com.xuxiao.config.SpringEventEntity;
import com.xuxiao.config.SpringEventForError;
import com.xuxiao.config.SpringEventForMessage;
import com.xuxiao.demo.Demo;
import javafx.application.Application;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author xuxiao
 */
@Component
public class EventListenerAnnotation {

    @Resource
    Demo demo;

    @EventListener
    public void onMessageListener(SpringEventForError springEventForError){
        print(springEventForError,"onMessageListener");
    }

    @EventListener
    @Async
    public void onMessageListenerAsync(SpringEventForError springEventForError){
        print(springEventForError,"onMessageListenerAsync");
    }

    @EventListener
    @Async
    public void onMessageListenerMessage(SpringEventForMessage springEventForMessage){
        System.out.println("线程名:"+Thread.currentThread().getName()+";打印："+springEventForMessage.getMessage());
    }

    @EventListener
    public void onSpringEventEntity(SpringEventEntity springEventEntity){
        // 此地方相较于自定义注解实现 sql 日志发送 或者 自定义注解实现 mq或者等邮箱发送可能会比较好
        System.out.println("线程名:"+Thread.currentThread().getName()+";打印："+springEventEntity.getSource());
    }

    private void print(SpringEventForError springEventForError,String name){
        System.out.println("方法名："+name+"线程名:"+Thread.currentThread().getName()+";打印："+springEventForError.getMessage());
    }

}
